package com.wzm.moviepic.fragment;

import java.io.File;
import java.util.ArrayList;

import net.tsz.afinal.FinalBitmap;

import com.wzm.bean.DownInfo;
import com.wzm.bean.HistoryBean;
import com.wzm.bean.MovieInfo;
import com.wzm.db.DBHelper;
import com.wzm.manager.AppConfig;
import com.wzm.manager.AppManager;
import com.wzm.manager.Tag;
import com.wzm.moviepic.AppApplication;
import com.wzm.moviepic.R;
import com.wzm.utils.FileUtils;
import com.wzm.utils.Logger;
import com.wzm.utils.UtilsTools;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class LFileAdapter extends BaseAdapter {
	
	private ArrayList<DownInfo> mList = null;
	private Context mContext = null;
	private LayoutInflater  mInflater;
//	private FinalBitmap fb = null;
//	private Animation zoomin;
	private Fragment item;
	LFileAdapter(Context context,ArrayList<DownInfo> list,Fragment _item)
	{
		item = _item;
		mContext = context;
		mList = list;
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);		
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return mList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View view = convertView;
		ViewHolder viewHolder = null;
		if(view == null)
		{
			view = mInflater.inflate(R.layout.local_item, parent, false);   
			viewHolder = new ViewHolder();			
			viewHolder.movie_name = (TextView)view.findViewById(R.id.tv_moviename);
			viewHolder.file_size = (TextView)view.findViewById(R.id.tv_filesize);
			viewHolder.btn_del = (Button)view.findViewById(R.id.btn_del);
			view.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) view.getTag();
		}
		final DownInfo mi = mList.get(position);
		viewHolder.movie_name.setText(mi.getMoviename());
		viewHolder.file_size.setText(FileUtils.FormetFileSize(mi.getFilesize()));
		//new Thread(new cutaRunnable(viewHolder.file_size,mi)).start();
		
		
		viewHolder.btn_del.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//删除文件，删除数据库
				if(FileUtils.DeleteMovieDir(mi.getMovieid()))
				{
					AppApplication.getInstance().getfMap().remove(mi.getMovieid());
					Intent uiintent = new Intent(Tag.UPDATELIST);
			        mContext.sendBroadcast(uiintent); 
			        uiintent = null;
					long rowid = DBHelper.getInstance(mContext).delDownInfo(mi.getMovieid());
					if(rowid==1)
					{
						mList.remove(mi);
						notifyDataSetChanged(); 						
						((LocalFileFragment)item).getDownInfo();
						
						Toast.makeText(mContext, "删除成功", Toast.LENGTH_SHORT).show();
					}
				}				
			}
		});
		return view;
	}
	
	
	
	private class ViewHolder {
        public TextView  movie_name;  
        public TextView  file_size;
        public Button btn_del;
    };
    
    class cutaRunnable implements Runnable{
    	
    	private TextView tv = null;
    	private DownInfo mi = null;
    	cutaRunnable(TextView _tv,DownInfo _mi)
    	{
    		tv = _tv;
    		mi = _mi;
    	}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			if(UtilsTools.hasSdcard()){
				File ff = new File(Environment.getExternalStorageDirectory().getPath()+AppConfig.MOVIESDIR+mi.getMovieid()+"/");		
				Logger.info(Environment.getExternalStorageDirectory().getPath()+AppConfig.MOVIESDIR+mi.getMovieid()+"/");
		        long filesize = 0;
				try {
					filesize = FileUtils.getFileSize(ff);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					//e.printStackTrace();
					Logger.error(e.getMessage());
				}
				if(filesize!=0)
				{
					String filesizestr = FileUtils.FormetFileSize(filesize);
					tv.setText(filesizestr);
					tv.setVisibility(View.VISIBLE);				
				}			
			}
		}
    	
    }

}
